﻿using DiaoPaiDaYin;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.UC.客户CRM.Service;
using DXApplicationTangche.UC.客户CRM.高德;
using DXApplicationTangche.UC.抖音出入库.Service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.订单一览新.销售确认单;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.下单MTO
{
    public partial class Form高德新建客户地址 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private GuideDTO guide = new GuideDTO();
        private List<GaodeAddressDetailDTO> gaodeAddressDetailDTOs匹配地址;
        private String address;
        private Boolean hasDetail = true;   //  是否有门牌号

        public Form高德新建客户地址()
        {
            InitializeComponent();
            initCombobox();
            this.ActiveControl = this.textBox地址搜索;
            this.IP默认地址();
        }
        public Form高德新建客户地址(DouyinAddressQueryDTO douyinAddressQueryDTO)
        {
            InitializeComponent();
            initCombobox();
            this.ActiveControl = this.textBox地址搜索;
            this.IP默认地址();
            this.修改进入(douyinAddressQueryDTO);
        }

        private void 修改进入(DouyinAddressQueryDTO douyinAddressQueryDTO)
        {
            //  赋值
            this.textBox联系电话.Text = douyinAddressQueryDTO.mobile;
            this.textBox客户姓名.Text = douyinAddressQueryDTO.customerName;
            this.address = douyinAddressQueryDTO.addressProvince 
                            + douyinAddressQueryDTO.addressCity 
                            + douyinAddressQueryDTO.addressDistrict 
                            + douyinAddressQueryDTO.addressDetail.Replace(douyinAddressQueryDTO.addressNumber,"");
            this.textBox已选地址.Text = this.address + douyinAddressQueryDTO.addressNumber;
            this.richTextBox详细地址.Text = douyinAddressQueryDTO.addressNumber;
            try
            {
                //显示图片
                this.pictureBox地址.LoadAsync("https://restapi.amap.com/v3/staticmap?location=" + douyinAddressQueryDTO.location + "&zoom=10&size=750*300&markers=mid,,A:" + douyinAddressQueryDTO.location + "&key=1e35c2196190d3f06060fb90f1c4ce30");
            }
            catch { }
            //  设置
            this.textBox联系电话.Enabled = false;
            this.textBox客户姓名.Enabled = false;
        }
        #region 省市县下拉框
        private void IP默认地址()
        {
            try
            {
                String ip = 高德地址Service.GetIP();
                GaodeIpDTO gaodeIpDTO = 高德地址Service.getGaodeIp(ip);
                this.comboBox省份.SelectedItem = gaodeIpDTO.province;
                this.comboBox城市.SelectedItem = gaodeIpDTO.city;
            }
            catch { }
        }
        private void initCombobox()
        {
            this.comboBox省份.Items.Clear();
            this.comboBox城市.Items.Clear();
            this.comboBox区县.Items.Clear();
            List<AreaDetailDTO> areaDetailDTOs = MTO订单Service.getAreaDetail("7");
            foreach (AreaDetailDTO item in areaDetailDTOs)
            {
                this.comboBox省份.Items.Add(item.name);
            }
        }

        private void comboBox省份_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.comboBox省份.Text))
            {
                return;
            }
            //  高德查询行政区
            this.guide = 高德地址Service.行政区查询(this.comboBox省份.Text);
            //  省份变化相关市、区变化
            this.comboBox城市.Items.Clear();
            this.comboBox区县.Items.Clear();
            foreach (DistrictDTO item in guide.districts[0].districts)
            {
                this.comboBox城市.Items.Add(item.name);
            }
        }
        
        private void comboBox城市_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.comboBox城市.Text))
            {
                return;
            }
            this.comboBox区县.Items.Clear();
            foreach (DistrictDTO item in guide.districts[0].districts)
            {
                if (this.comboBox城市.Text.Equals(item.name))
                {
                    foreach (DistrictDTO dTO in item.districts)
                    {
                        this.comboBox区县.Items.Add(dTO.name);
                    }
                    break;
                }
            }
            this.模糊查询();
        }
        private void comboBox区县_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.模糊查询();
        }
        #endregion

        private void gridView校验地址_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                String typecode = this.gridView校验地址.GetRowCellValue(this.gridView校验地址.FocusedRowHandle, "location").ToString();
                String district = this.gridView校验地址.GetRowCellValue(this.gridView校验地址.FocusedRowHandle, "district").ToString();
                String name = this.gridView校验地址.GetRowCellValue(this.gridView校验地址.FocusedRowHandle, "name").ToString();
                this.pictureBox地址.LoadAsync("https://restapi.amap.com/v3/staticmap?location=" + typecode + "&zoom=10&size=750*300&markers=mid,,A:" + typecode + "&key=1e35c2196190d3f06060fb90f1c4ce30");
                this.address = district + name;
                this.textBox已选地址.Text = this.address + " " + this.richTextBox详细地址.Text.Trim();
            }
            catch { }
        }
        private void textBox地址搜索_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.模糊查询();
            }
        }

        private void simpleButton搜索_Click(object sender, EventArgs e)
        {
            this.模糊查询();
        }

        private void 模糊查询()
        {
            //  查询
            GaodeAddressDTO gaodeAddressDTO = 高德地址Service.getCheckGaodeAddress(this.textBox地址搜索.Text.Trim(), this.comboBox城市.Text.Trim());
            this.gaodeAddressDetailDTOs匹配地址 = new List<GaodeAddressDetailDTO>();
            //  根据县区排序
            if (!String.IsNullOrWhiteSpace(this.comboBox区县.Text))
            {
                List<GaodeAddressDetailDTO> 区县 = new List<GaodeAddressDetailDTO>();
                List<GaodeAddressDetailDTO> 其他 = new List<GaodeAddressDetailDTO>();
                foreach (GaodeAddressDetailDTO gaodeAddressDetailDTO in gaodeAddressDTO.tips)
                {
                    //  返回地址不为空的才加入
                    if (!String.IsNullOrEmpty(gaodeAddressDetailDTO.district))
                    {
                        if ((this.comboBox省份.Text.Trim() + this.comboBox城市.Text.Trim() + this.comboBox区县.Text.Trim()).Equals(gaodeAddressDetailDTO.district))
                        {
                            //  当省市县和combo相同的时候
                            区县.Add(gaodeAddressDetailDTO);
                        }
                        else
                        {
                            //  当省市县不同的时候，key是“其他”
                            其他.Add(gaodeAddressDetailDTO);
                        }
                    }
                }
                gaodeAddressDetailDTOs匹配地址.AddRange(区县);
                gaodeAddressDetailDTOs匹配地址.AddRange(其他);
            }
            else 
            {
                if(null!= gaodeAddressDTO.tips)
                    foreach (GaodeAddressDetailDTO gaodeAddressDetailDTO in gaodeAddressDTO.tips)
                    {
                        //  返回地址不为空的才加入
                        if (!String.IsNullOrEmpty(gaodeAddressDetailDTO.district))
                        {
                            gaodeAddressDetailDTOs匹配地址.Add(gaodeAddressDetailDTO);
                        }
                    }
            }
            this.gridControl校验地址.DataSource = gaodeAddressDetailDTOs匹配地址;
            this.gridView校验地址.RefreshData();
            // 显示首条地址 
            this.显示首条地址();
        }
        private void 显示首条地址()
        {
            try
            {
                //显示首条地址
                this.pictureBox地址.LoadAsync("https://restapi.amap.com/v3/staticmap?location=" + gaodeAddressDetailDTOs匹配地址[0].location + "&zoom=10&size=750*300&markers=mid,,A:" + gaodeAddressDetailDTOs匹配地址[0].location + "&key=1e35c2196190d3f06060fb90f1c4ce30");
                this.address = gaodeAddressDetailDTOs匹配地址[0].district + gaodeAddressDetailDTOs匹配地址[0].name;
                this.textBox已选地址.Text = this.address + " " + this.richTextBox详细地址.Text.Trim();
            }
            catch { }
        }

        /// <summary>
        /// 判断手机号是否合法
        /// </summary>
        /// <param name="str_handset"></param>
        /// <returns></returns>
        private bool IsHandset(string str_handset)
        {
            Regex regex = new Regex("^1[345789]\\d{9}$");
            return regex.IsMatch(str_handset);
        }

        private void simpleButton重置_Click(object sender, EventArgs e)
        {
            this.initCombobox();
        }

        private void richTextBox详细地址_TextChanged(object sender, EventArgs e)
        {
            this.textBox已选地址.Text = this.address + " " + this.richTextBox详细地址.Text.Trim();
        }

        private void barButtonItem保存_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.textBox联系电话.Text.Trim()) ||
                String.IsNullOrWhiteSpace(this.textBox客户姓名.Text.Trim()) ||
                String.IsNullOrWhiteSpace(this.textBox已选地址.Text.Trim())
                )
            {
                MessageBox.Show("请完善客户姓名、手机号、地址信息");
                return;
            }
            if (hasDetail)
            {
                if (String.IsNullOrWhiteSpace(this.richTextBox详细地址.Text.Trim()))
                {
                    MessageBox.Show("请完善地址门牌号信息");
                    return;
                }
            }
            if (!this.IsHandset(this.textBox联系电话.Text.Trim()))
            {
                MessageBox.Show("手机号输入不合法，请再次确认");
                this.ActiveControl = this.textBox联系电话;
                return;
            }
            Form保存客户地址校验 form =
                new Form保存客户地址校验(
                    this.textBox客户姓名.Text.Trim(),
                    this.textBox联系电话.Text.Trim(),
                    this.textBox已选地址.Text.Trim(),
                    this.richTextBox详细地址.Text.Trim()
                );
            form.ShowDialog();
            if (form.if全关闭)
            {
                this.Close();
            }
            else
            {
                this.hasDetail = true;
                this.richTextBox详细地址.Enabled = true;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.address = null;
            this.textBox地址搜索.Clear();
            this.textBox已选地址.Clear();
            this.richTextBox详细地址.Clear();
            this.gridControl校验地址.DataSource = null;
            this.gridView校验地址.RefreshData();
            this.ActiveControl = this.richTextBox详细地址;
        }

        private void simpleButton无门牌_Click(object sender, EventArgs e)
        {
            this.hasDetail = false;
            this.richTextBox详细地址.Clear();
            this.richTextBox详细地址.Enabled = false;
        }
    }
}
